#include <iostream>
#include <string>
#include <unistd.h>
#include "Thread.hpp"
#include "lockGuard.hpp"

using namespace std;

// // 临界资源
int tickets = 1000;                                // 全局变量，共享对象
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 这是我在外边定义的锁


void threadRoutine(void *args)
{
    std::string message = static_cast<const char *>(args);
    while (true)
    {
        // pthread_mutex_lock(&mutex); // 加锁，是一个让不让你通过的策略
        {
            LockGuard lockguard(&mutex); // RAII 风格的锁
            if (tickets > 0)
            {
                usleep(2000);
                cout << message << " get a ticket: " << tickets-- << endl; // 临界区
            }
            else
            {
                break;
            }
        }

        // 我们抢完一张票的时候，我们还要有后续的动作
        // usleep(13);
    }
}

// void threadRun(void *args)
// {
//     std::string message = static_cast<const char *>(args);
//     int cnt = 10;
//     while (cnt)
//     {
//         cout << "我是一个线程, " << message << ", cnt: " << cnt-- << endl;
//         sleep(1);
//     }
// }

int main()
{
    Thread t1(1, threadRoutine, (void *)"hellozxn1");
    Thread t2(2, threadRoutine, (void *)"hellozxn2");
    Thread t3(3, threadRoutine, (void *)"hellozxn3");
    Thread t4(4, threadRoutine, (void *)"hellozxn4");

    t1.run();
    t2.run();
    t3.run();
    t4.run();

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    // cout << "thread name: " << t1.threadname() << " thread id: " << t1.threadid() << ",thread status: " << t1.status() << std::endl;
    // cout << "thread name: " << t2.threadname() << " thread id: " << t2.threadid() << ",thread status: " << t2.status() << std::endl;

    // t1.run();
    // t2.run();
    // cout << "thread name: " << t1.threadname() << " thread id: " << t1.threadid() << ",thread status: " << t1.status() << std::endl;
    // cout << "thread name: " << t2.threadname() << " thread id: " << t2.threadid() << ",thread status: " << t2.status() << std::endl;

    // t1.join();
    // t2.join();
    // cout << "thread name: " << t1.threadname() << " thread id: " << t1.threadid() << ",thread status: " << t1.status() << std::endl;
    // cout << "thread name: " << t2.threadname() << " thread id: " << t2.threadid() << ",thread status: " << t2.status() << std::endl;

    // pthread_mutex_t mutex;
    // pthread_mutex_init(&mutex, nullptr);

    // pthread_t tids[4];
    // int n = sizeof(tids)/sizeof(tids[0]);
    // for(int i = 0; i < n; i++)
    // {
    //     char name[64];
    //     snprintf(name, 64, "thread-%d", i+1);
    //     TData *td = new TData(name, &mutex);
    //     pthread_create(tids+i, nullptr, threadRoutine, td);
    // }

    // for(int i = 0; i < n; i++)
    // {
    //     pthread_join(tids[i], nullptr);
    // }

    // pthread_mutex_destroy(&mutex);
    return 0;
}
